package com.anysoft.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.bean.QueryFundListBean;
import com.anysoft.utils.StringUtils;

import java.util.ArrayList;

public class SearchAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<QueryFundListBean.DataBean> itemList;
    private LayoutInflater inflater;
    private String searchStr = "";

    public SearchAdapter(Context context, ArrayList<QueryFundListBean.DataBean> itemList) {
        this.itemList = itemList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setSearchStr(String searchStr) {
        this.searchStr = searchStr;
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoldler holdler = null;
        final QueryFundListBean.DataBean fundBean = itemList.get(position);
        if (convertView == null) {
            holdler = new ViewHoldler();
            convertView = inflater.inflate(R.layout.item_search, null);
            holdler.item_tv_fundcode = (TextView) convertView.findViewById(R.id.item_tv_fundcode);
            holdler.item_tv_fundtype = (TextView) convertView.findViewById(R.id.item_tv_fundtype);
            holdler.item_tv_fundname = (TextView) convertView.findViewById(R.id.item_tv_fundname);
            convertView.setTag(holdler);
        } else {
            holdler = (ViewHoldler) convertView.getTag();
        }

        if (fundBean.getFundcode().toUpperCase().contains(searchStr)) {
            SpannableStringBuilder fundcode = setSearchColor(fundBean.getFundcode());
            holdler.item_tv_fundcode.setText(fundcode);
            holdler.item_tv_fundtype.setText(fundBean.getFundtypeName());
            holdler.item_tv_fundname.setText(fundBean.getFundname());
        } else {
            SpannableStringBuilder fundname = setSearchColor(fundBean.getFundname());
            holdler.item_tv_fundcode.setText(fundBean.getFundcode());
            holdler.item_tv_fundtype.setText(fundBean.getFundtypeName());
            holdler.item_tv_fundname.setText(fundname);
        }

        return convertView;
    }

    public class ViewHoldler {
        TextView item_tv_fundcode;
        TextView item_tv_fundtype;
        TextView item_tv_fundname;
    }

    public SpannableStringBuilder setSearchColor(String changeStr) {
        if(!StringUtils.isNullOrEmpty(changeStr)) {
            changeStr = changeStr.toUpperCase();
        }
        int keyLength = searchStr.length();
        int index = changeStr.indexOf(searchStr);

        SpannableStringBuilder style = new SpannableStringBuilder(changeStr);
        style.setSpan(new ForegroundColorSpan(Color.RED), index, index + keyLength, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return style;
    }

}
